#pragma once
#include <vector>
#include <sstream>
#include <fstream>
#include "Student.h"

class StudentRegister
{
private:
	
public:
	vector<Student> registry;

	StudentRegister(void)
	{
	}

	~StudentRegister(void)
	{
	}

	void Add(Student student)
	{
		registry.push_back(student);
	}

	void Remove( int pos )
	{
		vector<Student>::iterator it1;

		it1 = registry.begin();
		advance(it1, pos-1);

		it1 = registry.erase(it1);
	}

	void Clear()
	{
		registry.clear();
	}

	string List()
	{
		ostringstream oss;

		for( auto &s : registry )
		{
			oss << s;
		}
		return oss.str();
	}

	void WriteToFile( string filename )
	{
		ofstream fout;

		try
		{
			fout.open( filename );
			for( auto &s : registry )
			{
				fout << s;
				s.SaveCourses( fout );
			}
			fout.close();
		}
		catch( exception e )
		{
			cerr << "Exception " << filename << endl;
			cerr << e.what() << endl;
		}

	}

	void ReadFromFile( string filename )
	{
		string str, strRow;
		ifstream fin;

		string studentString = "(firstName,lastName,enrollmentYear) ";
		string courseString = "(name,year) ";
		string resultString = "(maxPoint,point,maxGradeThreshold,mediumGradeThreshold,passGradeThreshold) ";

		int student = 0;
		int course = 0;

		try
		{
			fin.open( filename );
			registry.clear();

			while( getline( fin,strRow ) )
			{
				istringstream iss( strRow );
				string dummyStr;
				Student dummyStud;
				Course dummyCourse;
				Result dummyResult;
				

				getline(iss, dummyStr, '=');
				if( dummyStr == studentString )
				{
					iss >> dummyStud;
					if( dummyStud.GetFirstName() != "" )
					{
						Add( dummyStud );
						student += 1;
					}
				}
				else if( dummyStr == courseString )
				{
					iss >> dummyCourse;
					if( dummyCourse.GetName() != "" )
					{
						registry[student-1].LoadCourse(dummyCourse);
						course += 1;
					}
					else
					{
						course = 0;
					}
				}
				else if( dummyStr == resultString )
				{
					iss >> dummyResult;
					if( dummyResult.GetMaxPoint() != 0 )
					{
						registry[student-1].courses[course-1].LoadResult(dummyResult);
					}
				}

				
			}
			fin.close();
		}
		catch( exception e )
		{
			cerr << "Exception " << filename << endl;
			cerr << e.what() << endl;
		}
	}

	void AddStudent()
	{
		cout << string(50, '\n');  // Clear screen
		Student temp;
		cout << "Add student on following format: " << endl;
		cout << "(firstName,lastName,enrollmentYear)" << endl;
		cin >> temp;
		Add(temp);
	}

	void RemoveStudent()
	{
		int pos = 0;
		cout << string(50, '\n');  // Clear screen

		cout << "The registry contains:" << endl;
		cout << List();

		cout << "Select which position to remove ( 0 to go back ): ";
		cin >> pos;
		if( pos != 0 )
			if( pos > 0 && pos <= registry.size() )
				Remove(pos);
	}

	int SelectStudent()
	{
		int pos = 0;
		cout << string(50, '\n');  // Clear screen

		cout << "The registry contains:" << endl;
		cout << List();

		cout << "Select which student to add course/result to ( 0 to go back ): ";
		cin >> pos;

		if( pos >= 0 && pos < registry.size() )
			return pos;

		return 0;
	}
};

